<?php

use ttext\backend\blog\models\Article;
use wechat\models\WeConfig;
use wechat\models\WeContent;
use wechat\models\WeKeyword;
use yii\web\View;

/**
 * User: shutao
 * @var View $this
 * @var $model WeKeyword
 */
$this->title = '编辑';
$weConfigs = WeConfig::find()->where(['status' => WeConfig::STATUS_ACTIVE])->select(['id', 'title'])->asArray()->all();

function contentRecord($keywordType, $contentId)
{
    if ($contentId) {
        switch ($keywordType) {
            case WeKeyword::C_TYPE_CONTENT:
                return WeContent::find()->select(['id', 'type', 'title'])->where(['id' => $contentId])->asArray()->one();
            case WeKeyword::C_TYPE_BLOG_ARTICLE:
                return Article::find()->select(['id', 'type', 'title'])->where(['id' => $contentId])->asArray()->one();
        }
    }
    return ['id' => 0, 'type' => 0, 'title' => ''];
}

$content = contentRecord($model->c_type, $model->c_id);
?>

<div>
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="wc_id" class="layui-form-label">
                <?php echo $model->getAttributeLabel('wc_id') ?>
            </label>
            <div class="layui-input-inline">
                <select name="wc_id" id="wc_id">
                    <?php foreach ($weConfigs as $row) { ?>
                        <option value="<?php echo $row['id'] ?>"
                            <?php if ($row['id'] == $model->wc_id) {
                                echo 'selected';
                            } ?>
                        ><?php echo $row['title'] ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>

        <!-- 文本输入框 -->
        <div class="layui-form-item">
            <label for="keyword" class="layui-form-label">
                <span class="x-red">*</span> <?php echo $model->getAttributeLabel('keyword') ?>
            </label>
            <div class="layui-input-inline">
                <input type="text" name="keyword" id="keyword" required="" lay-verify="required"
                       placeholder=""
                       value="<?php echo $model->keyword ?>"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">支持微信系统所定义的关键词，如 subscribe, unsubscribe</div>
        </div>
        <!-- 日期指令 -->
        <div class="layui-form-item">
            <label for="open" class="layui-form-label">
                <?php echo $model->getAttributeLabel('start') ?>
            </label>
            <div class="layui-input-inline">
                <search-datetime no-init="1" <?php if ($model->start) {
                    echo 'value="' . $model->start . '"';
                } ?> id="start"></search-datetime>
            </div>
            <div class="layui-form-mid layui-word-aux">此关键词生效的时间，默认为0，一直有效</div>
        </div>
        <div class="layui-form-item">
            <label for="end" class="layui-form-label">
                <?php echo $model->getAttributeLabel('end') ?>
            </label>
            <div class="layui-input-inline">
                <search-datetime no-init="1" <?php if ($model->end) {
                    echo 'value="' . $model->end . '"';
                } ?> id="end"></search-datetime>
            </div>
            <div class="layui-form-mid layui-word-aux">此关键词的失效时间，默认为0，一直有效</div>
        </div>
        <!-- 下拉列表 -->
        <div class="layui-form-item">
            <label for="status" class="layui-form-label">
                <?php echo $model->getAttributeLabel('status') ?>
            </label>
            <div class="layui-input-inline">
                <select name="status" id="status">
                    <?php foreach (WeKeyword::statusText() as $key => $text) { ?>
                        <option value="<?php echo $key ?>"
                            <?php if ($key == $model->status) {
                                echo 'selected';
                            } ?>
                        ><?php echo $text ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>
        <!-- 回复类型-->
        <div class="layui-form-item">
            <label for="c_type" class="layui-form-label">
                <?php echo $model->getAttributeLabel('c_type') ?>
            </label>
            <div class="layui-input-inline">
                <select name="c_type" id="c_type">
                    <?php foreach (WeKeyword::cTypeText() as $key => $text) { ?>
                        <option value="<?php echo $key ?>"
                            <?php if ($key == $model->c_type) {
                                echo 'selected';
                            } ?>
                        ><?php echo $text ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="c_type" class="layui-form-label">
                <?php echo $model->getAttributeLabel('c_id') ?>
            </label>
            <div class="layui-input-inline">
                <input type="hidden" name="c_id" id="c_id" lay-verify="required|c_id"
                       value="<?php echo \ttcommon\base\Data::hasInt($content, 'id', 0) ?>">
                <a class="layui-btn layui-btn-sm layui-btn-normal" onclick="selectReplay()">选择</a>
                <span class="layui-form-mid layui-word-aux"
                      id="c_title"><?php echo \ttcommon\base\Data::hasValue($content, 'title', '') ?></span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label"></label>
            <input name="_csrf" type="hidden" id="_csrf" value="<?= \Yii::$app->request->csrfToken ?>">
            <?php if ($model->id) { ?>
                <input type="hidden" name="id" value="<?php echo $model->id ?>">
            <?php } ?>
            <button class="layui-btn" lay-filter='save' lay-submit="">保存</button>
        </div>
    </form>
</div>


<script>
    var cType = {'0': '内容回复', '1': '文章'};

    function selectReplay() {
        var type = document.getElementById('c_type').value; // 类型
        xlayer.open(cType[type], '?r=wechat/keyword/selected&type=' + type, function (data) {
            $('#c_id').val(data.id);
            document.getElementById('c_title').innerText = data.title;
            // console.log('callback', data);
        })
    }

    layui.use(['form', 'laydate', 'jquery'], function () {
        var form = layui.form, laydate = layui.laydate, $ = layui.$;

        form.verify({
            c_id: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (parseInt(value) < 1) {
                    return '当前关键词还没有关联回复内容';
                }
            }
        })
        //监听提交
        form.on('submit(save)', function (data) {
            $.ajax({
                type: "POST",
                url: '#',
                data: data.field,
                success: function (response, textStatus, xhr) {
                    layer.msg('成功', {icon: 6, time: 1000}, function () {
                        xlayer.close(response);
                    });
                },
                error: function (xhr, textStatus, errorThrown) {
                    // 1 正确, 2 错误(X), 3 询问，4 锁定, 5 不开心, 5 开心(笑脸)
                    layer.alert(textStatus + ':' + xhr.responseText, {icon: 2}, function (index) {
                        layer.close(index);
                    });
                },
                dataType: 'json'
            });
            return false;
        });
    });
</script>

